package com.zky.basic.controller;

import com.zky.util.AjaxResult;
import com.zky.util.FastDfsUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("fastDfs")
public class FastDfsController {

    @PostMapping
    //@RequestPart对复杂表单项的处理，默认name="file",与上传文件的name属性值一致
    public AjaxResult upload(@RequestPart MultipartFile file){
        try {
            String filename = file.getOriginalFilename();//xx.jpg
            String exeName = filename.substring(filename.lastIndexOf(".") + 1);//jpg
            Object upload = FastDfsUtils.upload(file.getBytes(), exeName);
            return new AjaxResult(true,upload);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"添加失败！！！");
        }
    }
    @DeleteMapping
    public AjaxResult deleteFile(@RequestParam("path")String path){
        try {
            path = path.substring(1);
            String groupName = path.substring(0, path.indexOf("/"));
            String fileName = path.substring(path.indexOf("/")+1);
            FastDfsUtils.delete(groupName,fileName);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


}
